<?php

namespace App\Models\Api;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class News extends Model
{
    //
    protected $table = 'news';//表

    protected $hidden = ['cid'];

    public function getCidsAttribute()
    {
        return explode(';', $this->cid);
    }

    public function news_category()
    {
        return $this->hasOne(NewsCategory::class, 'category');
    }

    public function news_img_exist()
    {
        return $this->hasMany(NewsImgExist::class, 'news_id')
            ->where('img_state', 1);
    }

    public function getCreatedAtAttribute($value)
    {
        return Carbon::parse($value)->format('Y-m-d');
    }

    public function getImgAttribute()
    {
        return count($this->news_img_exist) != 0 ?
            'newsimg/' . $this->news_img_exist->first()->img_url : '';
    }
}
